package com.senjoeson.teamfriends.adapter;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.senjoeson.teamfriends.R;
import com.senjoeson.teamfriends.activity.AddressInfoActivity;
import com.senjoeson.teamfriends.bean.FriendMessage;
import com.senjoeson.teamfriends.bean.User;
import com.senjoeson.teamfriends.utils.ALog;
import com.senjoeson.teamfriends.utils.GlideUtils;
import com.senjoeson.teamfriends.utils.MyToast;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;
import de.hdodenhof.circleimageview.CircleImageView;

public class TeamMessageAdapter extends RecyclerView.Adapter {


    private FragmentActivity mFragmentActivity;
    private List<FriendMessage> mFriendMessages;

    public TeamMessageAdapter(FragmentActivity activity) {
        this.mFragmentActivity = activity;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.teamfriend_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        final FriendMessage friendMessage = mFriendMessages.get(position);
        viewHolder.mPublishdate.setText(friendMessage.getCreatedAt());
        viewHolder.mContent.setText(friendMessage.getContent());
        int like_num = friendMessage.getLike_num();
        if (like_num > 0) {
            viewHolder.mLove.setText(like_num);
            viewHolder.mLove.setVisibility(View.VISIBLE);
        } else {
            viewHolder.mLove.setText("");
            viewHolder.mLove.setVisibility(View.GONE);
        }
        int comment_num = friendMessage.getComment_num();
        if (comment_num > 0) {
            viewHolder.mMessage.setText(comment_num);
            viewHolder.mMessage.setVisibility(View.VISIBLE);
        } else {
            viewHolder.mMessage.setText("");
            viewHolder.mMessage.setVisibility(View.GONE);
        }
        viewHolder.mNickname.setText(friendMessage.getAuthor_name());
        viewHolder.mLocation.setText(friendMessage.getAddress());
        if (friendMessage.getPicture() != null) {
            GlideUtils.loadImage(holder.itemView.getContext(), friendMessage.getPicture().getFileUrl(), viewHolder.mPicture);
        }
        GlideUtils.loadImage(holder.itemView.getContext(), friendMessage.getUser_icon(), viewHolder.mPortrait);
        viewHolder.mLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {  //点击查看位置信息
                LatLng address_latlan = friendMessage.getAddress_latlan();
                Intent intent = new Intent(mFragmentActivity, AddressInfoActivity.class);
                intent.putExtra("LatLng", address_latlan);
                mFragmentActivity.startActivity(intent);
            }
        });
        viewHolder.mShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyToast.showToast("暂不实现");
            }
        });
        viewHolder.mTurnto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyToast.showToast("暂不实现");
            }
        });
        viewHolder.mMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        viewHolder.mLove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (TextUtils.equals(BmobUser.getCurrentUser(User.class).getObjectId(), friendMessage.getUser_id())) {
                    final AlertDialog.Builder builder = new AlertDialog.Builder(viewHolder.itemView.getContext());
                    builder.setMessage("你确定要删除这条消息么？").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            friendMessage.delete(new UpdateListener() {
                                @Override
                                public void done(BmobException e) {
                                    if (e == null) {
                                        MyToast.showToast("删除成功！");
                                        mFriendMessages.remove(position);
                                        notifyDataSetChanged();
                                    } else {
                                        ALog.d("删除失败！");
                                    }
                                }
                            });
                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            builder.create().dismiss();
                        }
                    });
                    builder.create().show();
                }


                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mFriendMessages != null ? mFriendMessages.size() : 0;
    }

    public void setDatas(List<FriendMessage> list) {
        mFriendMessages = list;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.portrait)
        CircleImageView mPortrait;
        @BindView(R.id.nickname)
        TextView mNickname;
        @BindView(R.id.publishdate)
        TextView mPublishdate;
        @BindView(R.id.share)
        ImageView mShare;
        @BindView(R.id.content)
        TextView mContent;
        @BindView(R.id.picture)
        ImageView mPicture;
        @BindView(R.id.location)
        TextView mLocation;
        @BindView(R.id.turnto)
        ImageView mTurnto;
        @BindView(R.id.message)
        TextView mMessage;
        @BindView(R.id.love)
        TextView mLove;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


    /*public void showShare() {
        new ShareAction(mFragmentActivity)
                .setPlatform(SHARE_MEDIA.QQ)//传入平台
                .withText("hello")//分享内容
                // .setCallback(umShareListener)//回调监听器
                .share();
    }*/
}
